package com.zwface.demo.base;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import org.greenrobot.eventbus.EventBus;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;

/**
 * created by 张中文
 * on 2020/2/2
 * 描述：框架Activity
 *
 *  @method : 配置部分Activity  —— onActivityConfigure
 *  @method : 设置界面  —— onLayoutId
 *  @method : 初始化数据  —— onInitData
 *  @method : 初始化监听  —— onInitListener
 *  @method : 操作逻辑  —— onInitComponent
 *
 *  @method : 跳转到一个新的Activity，不结束当前的Activity  —— gotoActivityNotFinish
 *  @method : 跳转到一个新的Activity，结束当前的Activity  —— gotoActivity
 *  @method : 跳转到一个Activity，需要回调结果 —— gotoActivityForResult
 */
public abstract class FrameActivity<T extends ViewDataBinding> extends AppCompatActivity
{
    public Activity context;
    public T binding;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = this;

        //配置部分Activity
        onActivityConfigure();

        //设置界面
        //设置界面
        binding = DataBindingUtil.setContentView(this, onLayoutId());
        onInitViewDataBinding(binding);
        setContentView(binding.getRoot());

        if (onSetEventBus()) {
            EventBus.getDefault().register(this);
        }

        if (getIntent().getExtras() != null) {
            onReceiveBundleFromPre(getIntent().getExtras());
        }

        //初始化数据
        onInitData(savedInstanceState);

        //初始化监听
        onInitListener();

        //操作
        onInitComponent();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (onSetEventBus()) {
            EventBus.getDefault().unregister(this);
        }
    }

    abstract public void onActivityConfigure();

    abstract public Boolean onSetEventBus();

    abstract public void onReceiveBundleFromPre(Bundle bundle);

    abstract public int onLayoutId();

    abstract public void onInitViewDataBinding(T binding);

    abstract public void onInitData(Bundle savedInstanceState);

    abstract public void onInitListener();

    abstract public void onInitComponent();

    /**
     * 跳转到一个新的Activity，不结束当前的Activity
     */
    public void gotoActivityNotFinish(Class c, Bundle bundle) {
        Intent intent = new Intent(this,c);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        this.startActivity(intent);
    }

    /**
     *  跳转到一个新的Activity，并将当前的Activity结束
     */
    public void gotoActivity(Class c, Bundle bundle) {
        Intent intent = new Intent(this,c);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        this.startActivity(intent);
        finish();
    }


    /**
     * 跳转到一个Activity，需要回调结果
     */
    public void gotoActivityForResult(int requestCode, Class c, Bundle bundle) {
        Intent intent = new Intent(this,c);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivityForResult(intent, requestCode);
    }
}
